package cn.well.cloud.modular.service.impl.thread;

import cn.stylefeng.roses.core.util.SpringContextHolder;
import cn.well.cloud.base.wechat.api.impl.TemplateMessage;
import cn.well.cloud.base.wechat.model.NoticeInfo;
import cn.well.cloud.modular.model.NoticeLog;
import cn.well.cloud.modular.model.UserInfo;
import cn.well.cloud.modular.service.impl.NoticeLogServiceImpl;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

@Slf4j
public class SendNoticeThread implements Runnable{

    private NoticeLogServiceImpl noticeLogService = SpringContextHolder.getBean("noticeLogServiceImpl");

    /**
     * 构造方法传参,要发送的用户集合
     */
    private  List<UserInfo> userList;
    /**
     * 构造方法传参,要发送的课程信息
     */
    private NoticeInfo noticeInfo ;
    /**
     * 构造方法传参,接口调用凭证
     */
    private String token ;
    /**
     * 构造方法传参,通知记录
     */
    private NoticeLog noticeLog ;

    private CountDownLatch begin;

    private CountDownLatch end;

    /**
     * 模板消息工具
     */
    private TemplateMessage templateMessage = new TemplateMessage();

    //创建个构造函数初始化 list,和其他用到的参数
    public SendNoticeThread(NoticeInfo noticeInfo, List<UserInfo> userList,String token,NoticeLog noticeLog,CountDownLatch begin, CountDownLatch end) {
        this.noticeInfo =noticeInfo;
        this.userList =userList;
        this.token = token;
        this.noticeLog = noticeLog;
        this.begin = begin;
        this.end = end;
    }

    @Override
    public void run() {
        try {
            for (int i = 0; i < userList.size(); i++) {
                String openId = userList.get(i).getOpenId();
                Long userInfoId = userList.get(i).getUserInfoId();
                NoticeInfo newNoticeInfo = new NoticeInfo();
                newNoticeInfo = this.noticeInfo;
                newNoticeInfo.setTouser(openId);
                Object object = null;
                try {
                    object =  templateMessage.sendTempMessage(token, newNoticeInfo);
                    JSONObject json = JSONObject.parseObject(object.toString());
                    int errcode = json.getIntValue("errcode");
                    if(errcode ==0){
                        Long msgid = json.getLong("msgid");
                        //回写通知记录
                        NoticeLog newnoticeLog = new NoticeLog();
                        newnoticeLog.setCreateUser(noticeLog.getCreateUser());
                        newnoticeLog.setMsgId(String.valueOf(msgid));
                        newnoticeLog.setTemplateId(noticeLog.getTemplateId());
                        newnoticeLog.setWxType(noticeLog.getWxType());
                        newnoticeLog.setUserInfoId(userInfoId);
                        newnoticeLog.setCourseId(noticeLog.getCourseId());;
                        noticeLogService.save(newnoticeLog);
                        log.info("通知记录成功");
                    }
                } catch (IOException e) {
                    log.error("发送失败");
                    log.error(JSONObject.toJSONString(object));
                    e.printStackTrace();
                }

                log.info(Thread.currentThread().getName());
            }

            //执行完让线程直接进入等待
            begin.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            //这里要主要了，当一个线程执行完 了计数要减一不然这个线程会被一直挂起
            // ，end.countDown()，这个方法就是直接把计数器减一的
            end.countDown();
        }
    }
}
